package com.dlg.user.filter;

import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域访问 过滤器
 * 
 * @author xpp
 * @date 2017年4月19日 下午7:41:05
 */
/*@Order(2)
@WebFilter(urlPatterns = "/*", filterName = "crossDomainFilter")*/
public class CrossDomainFilter extends OncePerRequestFilter {

	@Resource(name = "springContextHolder")
	private SpringContextHolder springContextHolder;

	public void setSpringContextHolder(SpringContextHolder springContextHolder) {
		this.springContextHolder = springContextHolder;
	}

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Allow-Methods", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE");
		response.addHeader("Access-Control-Allow-Credentials", "true");
		response.addHeader("Access-Control-Max-Age", springContextHolder.getProperty("access_control_max_age"));
		filterChain.doFilter(request, response);
		
		
	}

}
